package god.dream.comment.controller;

import god.dream.comment.model.Comment;
import god.dream.comment.service.CommentService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class CommentController extends MultiActionController {
	@Autowired
	private CommentService commentService;
	
	public ModelAndView listComments(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<Comment> commentList = commentService.findCommentList();
		System.out.println("sar");
		ModelAndView mav = new ModelAndView("list_comments");
		mav.addObject("comments", commentList);

		return mav;
	}
	public ModelAndView deleteComment(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		String uid = request.getParameter("uid");
		if(id != null)
			commentService.removeComment(Integer.parseInt(id));
		List<Comment> commentList = commentService.findCommentList();
		System.out.println("sar");
		ModelAndView mav = new ModelAndView("redirect:/cont/selectUser.htm");
		mav.addObject("id", uid);

		return mav;
	}
	public ModelAndView selectComment(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		Comment comment = new Comment();
		if(id != null)
			comment = commentService.findComment(Integer.parseInt(id));
		System.out.println("sar");
		ModelAndView mav = new ModelAndView("select_comment");
		mav.addObject("comment", comment);

		return mav;
	}
	public ModelAndView insertComment(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String uid = request.getParameter("uid");
		Comment comment = new Comment();
		comment.setUid(Integer.parseInt(uid));
		comment.setCommValue(request.getParameter("commValue"));
		commentService.addComment(comment);
		System.out.println("sar");
		ModelAndView mav = new ModelAndView("redirect:/cont/selectUser.htm");
		mav.addObject("id", uid);

		return mav;
	}
}
